package com.rankeiot.core.inner;

import com.rankeiot.core.data.response.Resp;
import com.rankeiot.core.exception.AuthException;
import com.rankeiot.core.exception.BusinessException;
import com.rankeiot.core.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

@RestControllerAdvice
@Slf4j
public class RestControllerExceptionHandler {
    @ExceptionHandler(value = NoResourceFoundException.class)
    public ResponseEntity handleNotFound(NoResourceFoundException e){
       //do nothing
        return ResponseEntity.notFound().build();
    }

    @ExceptionHandler(value = BindException.class)
    public Resp handleBindException(BindException e) {
        List<FieldError> allErrors = e.getFieldErrors();
        StringBuilder sb = new StringBuilder();
        for (FieldError errorMessage : allErrors) {
            sb.append(errorMessage.getField()).append(": ").append(errorMessage.getDefaultMessage()).append(", ");
        }
        Resp resp = Resp.ok(sb.toString());
        resp.setSuccess(false);
        resp.setCode(400);
        return resp;
    }

    @ExceptionHandler(value = BusinessException.class)
    public Object handleBusinessException(BusinessException e) {
        log.info("",e);
        Resp resp = Resp.ok(e.getMessage());
        resp.setSuccess(false);
        resp.setCode(e.getCode());
        return resp;
    }
    @ExceptionHandler(value = AuthException.class)
    public ResponseEntity<Resp> handleAuthException(AuthException e){
        Resp resp = Resp.ok(e.getMessage());
        resp.setSuccess(false);
        resp.setCode(e.getCode());
        return ResponseEntity.status(e.getCode()).body(resp);
    }
    @ExceptionHandler(value = Throwable.class)
    public ResponseEntity<String> handleThrowable(Throwable e){
        log.error("",e);
        Resp resp = Resp.ok(e.getMessage());
        resp.setSuccess(false);
        resp.setCode(500);
        return ResponseEntity.status(500).body(JSONUtil.toJSONString(resp));
    }
    @ExceptionHandler(value = MultipartException.class)
    public  ResponseEntity<Resp>  handleMultipartException(MultipartException e){
        if(e instanceof MaxUploadSizeExceededException){
            MaxUploadSizeExceededException me=(MaxUploadSizeExceededException)e;
            double size= me.getMaxUploadSize()*100D/(1024*1024);
            String sizeStr="文件上传错误,上传文件过大";
            if(size>0){
               sizeStr= "文件上传错误，上传文件最大为"+new DecimalFormat("###.00M").format(size);
            }
            Resp resp = Resp.ok(sizeStr);
            resp.setSuccess(false);
            resp.setCode(400);
            return ResponseEntity.status(400).body(resp);
        }
        log.error("",e);
        Resp resp = Resp.ok(e.getMessage());
        resp.setSuccess(false);
        resp.setCode(400);
        return ResponseEntity.status(400).body(resp);
    }
}
